package approve.action;

import project.active.L_list_subprolistmenu;
import project.active.ListProjectManager;
import project.active.LogoutActivity;
import project.active.Newproject;
import project.active.Projectlist;
import project.active.Projectlist_tc;
import model.pojo.Employee;
import models.util.UtilInitLang;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class MainProject extends Activity{
	TextView logout,username;
	String lang = "EN",database;
	Bundle language;
	Employee emp= new Employee();
	UtilInitLang utilang=new UtilInitLang();

	ListView lstMainProject;
	L_list_subprolistmenu admenu;
	
	public UtilInitLang getUtilang() {
		return utilang;
	}

	public void setUtilang(UtilInitLang utilang) {
		this.utilang = utilang;
	}


	public String getDatabase() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			database = extras.getString("database");
		}
		return database;
	}

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.mainproject);
		lstMainProject = (ListView)findViewById(R.id.listMainProject);
		
		
		language = getIntent().getExtras().getBundle(getLang());

		Intent currentIntent = this.getIntent();
		emp=(Employee) getIntent().getExtras().get("em");
		emp = (Employee)currentIntent.getSerializableExtra("em");

		//		    showDialogMessage();
		logout=(TextView)findViewById(R.id.logout);
		username=(TextView)findViewById(R.id.username);
		logout.setText(language.getString("Logout"));

		logout.setOnClickListener(listenerLogout);
		username.setText(emp.getUserName());
		
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainProject.this,android.R.layout.simple_list_item_1,listAllProject(getLang()));
		admenu = new L_list_subprolistmenu(MainProject.this, listAllProject(getLang()));
//		listproject.setAdapter(adapter); 
		lstMainProject.setAdapter(admenu); 
		
		
		lstMainProject.setOnCreateContextMenuListener(MainProject.this);
		OnItemClickListener listenerProject = new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				String item = arg0.getItemAtPosition(arg2).toString();
				if(emp.getIdrole()==1){
//					if(item.equals("Ã¬Æ’Ë† Ã­â€�â€žÃ«Â¡Å“Ã¬Â ï¿½Ã­Å Â¸") || item.equals("DÃ¡Â»Â± ÃƒÂ¡n mÃ¡Â»â€ºi") || item.equals("New Project")){
//						Intent intent=new Intent(MainProject.this,Newproject.class);
//						intent.putExtra("lang", getLang());
//						intent.putExtra("database", getDatabase());
//						intent.putExtra("em",emp);
//						//intent.putExtra("proaction", "newpro");
//						intent.putExtras(getIntent().getExtras());
//						startActivityForResult(intent, 0);
//					}else if(item.equals("List Project") || item.equals("Ã­â€�â€žÃ«Â¡Å“Ã¬Â ï¿½Ã­Å Â¸ Ã«Â¦Â¬Ã¬Å Â¤Ã­Å Â¸") || item.equals("Danh sÃƒÂ¡ch dÃ¡Â»Â± ÃƒÂ¡n")){
//						Intent intent=new Intent(MainProject.this,Projectlist.class);
//						intent.putExtra("lang", getLang());
//						intent.putExtra("database", getDatabase());
//						intent.putExtra("em",emp);
//						intent.putExtras(getIntent().getExtras());
//						startActivityForResult(intent, 1);
//					}
					if(item.equals(language.getString("new_project"))){
						Intent intent=new Intent(MainProject.this,Newproject.class);
						intent.putExtra("lang", getLang());
						intent.putExtra("database", getDatabase());
						intent.putExtra("em",emp);
						//intent.putExtra("proaction", "newpro");
						intent.putExtras(getIntent().getExtras());
						startActivityForResult(intent, 0);
					}else if(item.equals(language.getString("ac_SUProjectList"))){
						Intent intent=new Intent(MainProject.this,Projectlist.class);
						intent.putExtra("lang", getLang());
						intent.putExtra("database", getDatabase());
						intent.putExtra("em",emp);
						intent.putExtras(getIntent().getExtras());
						startActivityForResult(intent, 1);
					}
				}else{
					if(item.equals("List Manager") || item.equals("List mana") || item.equals("Du an quan ly")){
						Intent intent = new Intent(MainProject.this,ListProjectManager.class);
						intent.putExtra("lang", getLang());
						intent.putExtra("database", getDatabase());
						intent.putExtra("em",emp);
						intent.putExtras(getIntent().getExtras());
						startActivityForResult(intent,0);
						
					}else if(item.equals("List Member") || item.equals("List mem") || item.equals("Du an thanh vien")){
						Intent intent=new Intent(MainProject.this,Projectlist_tc.class);
						intent.putExtra("lang", getLang());
						intent.putExtra("database", getDatabase());
						intent.putExtra("em",emp);
						intent.putExtras(getIntent().getExtras());
						startActivityForResult(intent, 0);
					}
				}
				
			}
			
		};
		lstMainProject.setOnItemClickListener(listenerProject );
	}
	
	public String getLang() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			lang = extras.getString("lang");
		}
		return lang;
	}

	private OnClickListener listenerLogout=new OnClickListener() {

		public void onClick(View v) {
			LoginActivity.flagupdateversion=false;

			Intent i=new Intent(MainProject.this, LogoutActivity.class);
			i.putExtra("database", getDatabase());
			startActivity(i);
		}
	};
	
	public String[] listAllProject(String value) {
		String[] menu = null;
		if(emp.getIdrole()==1){
//			if (value.equals("EN")) {
//				menu = getResources().getStringArray(R.array.project_en_admin);
//			} else if (value.equals("KR")) {
//				menu = getResources().getStringArray(R.array.project_kr_admin);
//			} else {
//				menu = getResources().getStringArray(R.array.project_vn_admin);
//			}
			menu= new String[2];
			if(language.getString("new_project")!=null){
				menu[0]=language.getString("new_project");
			}else
				menu[0]="";
			
			if(language.getString("ac_SUProjectList")!=null){
				menu[1]=language.getString("ac_SUProjectList");
			}else
				menu[1]="";
			
		}else{
			if (value.equals("EN")) {
				menu = getResources().getStringArray(R.array.project_en_member);
			} else if (value.equals("KR")) {
				menu = getResources().getStringArray(R.array.project_kr_member);
			} else {
				menu = getResources().getStringArray(R.array.project_vn_member);
			}
		}
		
		return menu;
	}
}
